/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.platform.controller;

import cn.hutool.core.date.DateUtil;
import com.yami.shop.bean.enums.FlowTimeTypeEnum;
import com.yami.shop.bean.param.FlowUserAnalysisParam;
import com.yami.shop.platform.config.FlowUserAnalysisType;
import com.yami.shop.service.FlowUserAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * @author lgh on 2018/11/26.
 */
@RestController
@RequestMapping("/platform/flowUserAnalysis")
public class FlowUserAnalysisController {

    @Autowired
    private FlowUserAnalysisService flowUserAnalysisService;


    /**
     * 获取会员分析数据
     * @param flowUserAnalysisParam
     * @return
     */
    @GetMapping("/getUserAnalysisData")
    public ResponseEntity<FlowUserAnalysisParam> getUserAnalysisData(FlowUserAnalysisParam flowUserAnalysisParam) {
        Integer type = flowUserAnalysisParam.getType();
        if (!Objects.equals(type,FlowUserAnalysisType.CUSTOM.value())){
            int day = 0;
            if (type == 1){
                day = -7;
            }else {
                day = -30;
            }
            Date endTime = DateUtil.beginOfDay(new Date());
            flowUserAnalysisParam.setEndTime(endTime);
            flowUserAnalysisParam.setStartTime(DateUtil.offsetDay(endTime,day));
            flowUserAnalysisParam.setStart(flowUserAnalysisParam.getStartTime().getTime());
            flowUserAnalysisParam.setEnd(DateUtil.offsetDay(endTime,-1).getTime());
        }else {
            if (Objects.isNull(flowUserAnalysisParam.getEnd()) || Objects.isNull(flowUserAnalysisParam.getStart())){
                flowUserAnalysisParam.setEndTime(new Date());
                flowUserAnalysisParam.setStartTime(new Date());
            }else {
                flowUserAnalysisParam.setEndTime(new Date(flowUserAnalysisParam.getEnd()));
                flowUserAnalysisParam.setStartTime(new Date(flowUserAnalysisParam.getStart()));
            }
        }
        flowUserAnalysisService.getUserAnalysisData(flowUserAnalysisParam);
        return ResponseEntity.ok(flowUserAnalysisParam);
    }
}
